<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="a:">
          <el-input
            v-model="formData.anumber"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="d:">
          <el-input
            v-model="formData.dnumber"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="suanfa" type="primary">灰太狼1号</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      border
      ref="multipleTable"
      stripe
      tooltip-effect="dark"
      show-summary
      :height="600"
    >
      <el-table-column label="ID" prop="ID" align="center"></el-table-column>

      <el-table-column label="number1字段" width="120" align="center">
        <template slot-scope="scope">
          <div
            v-if="
              fudongMessage.hasOwnProperty(
                scope.row.ID + '-' + scope.row.number1
              )
            "
          >
            <el-popover trigger="hover" placement="top">
              <p>
                a期数:
                {{ fudongMessage[scope.row.ID + "-" + scope.row.number1]["a"] }}
              </p>
              <p>
                相差期数:
                {{
                  fudongMessage[scope.row.ID + "-" + scope.row.number1]["cha"]
                }}
              </p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.number1 }}</el-tag>
              </div>
            </el-popover>
          </div>
          <div v-else>{{ scope.row.number1 }}</div>
        </template>
      </el-table-column>

      <el-table-column label="number2字段" width="120" align="center">
        <template slot-scope="scope">
          <div
            v-if="
              fudongMessage.hasOwnProperty(
                scope.row.ID + '-' + scope.row.number2
              )
            "
          >
            <el-popover trigger="hover" placement="top">
              <p>
                a期数:
                {{ fudongMessage[scope.row.ID + "-" + scope.row.number2]["a"] }}
              </p>
              <p>
                相差期数:
                {{
                  fudongMessage[scope.row.ID + "-" + scope.row.number2]["cha"]
                }}
              </p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.number2 }}</el-tag>
              </div>
            </el-popover>
          </div>
          <div v-else>{{ scope.row.number2 }}</div>
        </template>
      </el-table-column>

      <el-table-column label="number3字段" width="120" align="center">
        <template slot-scope="scope">
          <div
            v-if="
              fudongMessage.hasOwnProperty(
                scope.row.ID + '-' + scope.row.number3
              )
            "
          >
            <el-popover trigger="hover" placement="top">
              <p>
                a期数:
                {{ fudongMessage[scope.row.ID + "-" + scope.row.number3]["a"] }}
              </p>
              <p>
                相差期数:
                {{
                  fudongMessage[scope.row.ID + "-" + scope.row.number3]["cha"]
                }}
              </p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.number3 }}</el-tag>
              </div>
            </el-popover>
          </div>
          <div v-else>{{ scope.row.number3 }}</div>
        </template>
      </el-table-column>

      <el-table-column label="number4字段" width="120" align="center">
        <template slot-scope="scope">
          <div
            v-if="
              fudongMessage.hasOwnProperty(
                scope.row.ID + '-' + scope.row.number4
              )
            "
          >
            <el-popover trigger="hover" placement="top">
              <p>
                a期数:
                {{ fudongMessage[scope.row.ID + "-" + scope.row.number4]["a"] }}
              </p>
              <p>
                相差期数:
                {{
                  fudongMessage[scope.row.ID + "-" + scope.row.number4]["cha"]
                }}
              </p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.number4 }}</el-tag>
              </div>
            </el-popover>
          </div>
          <div v-else>{{ scope.row.number4 }}</div>
        </template>
      </el-table-column>

      <el-table-column label="number5字段" width="120" align="center">
        <template slot-scope="scope">
          <div
            v-if="
              fudongMessage.hasOwnProperty(
                scope.row.ID + '-' + scope.row.number5
              )
            "
          >
            <el-popover trigger="hover" placement="top">
              <p>
                a期数:
                {{ fudongMessage[scope.row.ID + "-" + scope.row.number5]["a"] }}
              </p>
              <p>
                相差期数:
                {{
                  fudongMessage[scope.row.ID + "-" + scope.row.number5]["cha"]
                }}
              </p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.number5 }}</el-tag>
              </div>
            </el-popover>
          </div>
          <div v-else>{{ scope.row.number5 }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="time"
        prop="time"
        align="center"
      ></el-table-column>
      <el-table-column label="所属项目" prop="projectId" align="center">
        <template slot-scope="scope">
          {{ filterDict(scope.row.projectId, "sys_project_id") }}
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="统计信息"
      :visible.sync="dialogTongjiFormVisible"
      :before-close="closeTongjiDialog"
    >
      <span>统计信息：注意此处统计为a数字后10期内奖项值出现此处</span>
      <span
        >灰太狼1号：用户输入a数字，输入d。将计算a上方10行所有位置-d的值出现在下10期中的次数，列的那个数字+d(可为负数)。
      </span>
      <el-table
        :data="tongjiTabledata"
        style="width: 100%"
        :default-sort="{ prop: 'value', order: 'descending' }"
      >
        <el-table-column prop="key1" label="往上期数" width="180">
        </el-table-column>
        <el-table-column prop="key2" label="列数" width="180">
        </el-table-column>
        <el-table-column prop="key3" label="往下期数" width="180">
        </el-table-column>
        <el-table-column prop="value" label="次数" width="180" sortable>
        </el-table-column>

        <el-table-column label="a出现次数" width="120" align="center">
          <template>
            <div>{{ anumbercount }}</div>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeTongjiDialog">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSysCaipiaoNumbersList,
  getLengSysCaipiaoNumbers,
} from "@/api/sysCaipiaoNumbers"; //  此处请自行替换地址
import { formatTimeToStr } from "@/utils/date";
import infoList from "@/mixins/infoList";
export default {
  name: "SysCaipiaoNumbers",
  mixins: [infoList],
  data() {
    return {
      listApi: getSysCaipiaoNumbersList,
      dialogFormVisible: false,
      dialogTongjiFormVisible: false,
      type: "",
      multipleSelection: [],
      sys_project_idOptions: [],
      formData: {
        time: new Date(),
        number1: "",
        number2: "",
        number3: "",
        number4: "",
        number5: "",
        projectId: 0,
        project_id: 539,
        qishu: 3000,
        anumber: "20",
        dnumber: "-4",
        tablex: 10, //往上数
        tabley: 10,
        tabley2: 10, //往下数
      },

      formlenreData: [],
      anumberArray: [], //方便标上不同颜色
      bnumberArray: [], //方便标上不同颜色
      cnumberArray: [], //方便标上不同颜色
      countChazhi: {}, //统计每个出现的次数
      fudongMessage: {}, //方便具体期数与数字的高亮
      tongjiTabledata: [], //
      anumbercount: 0,
      //   page: 1,
      //   pageSize: 100000,
    };
  },
  filters: {
    formatDate: function (time) {
      if (time != null && time != "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    },
    formatBoolean: function (bool) {
      if (bool != null) {
        return bool ? "是" : "否";
      } else {
        return "";
      }
    },
  },
  methods: {
    //条件搜索前端看此方法
    onSubmit() {
      this.page = 1;
      this.pageSize = 10;
      this.getTableData();
      this.tableData = this.getObjsToArray(this.tableData);
      window.console.log(1, this.tableData);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    tableRowClassName({ row, rowIndex }) {
      console.log(row, rowIndex);
      if (rowIndex === 1) {
        return "warning-row";
      } else if (rowIndex === 3) {
        return "success-row";
      }
      return "";
    },

    closeDialog() {
      this.dialogFormVisible = false;
      this.formData = {
        time: new Date(),
        number1: "",
        number2: "",
        number3: "",
        number4: "",
        number5: "",
        projectId: 0,
      };
    },

    closeTongjiDialog() {
      //关闭统计的弹窗
      this.dialogTongjiFormVisible = false;
    },
    openDialog() {
      this.type = "create";
      this.dialogFormVisible = true;
    },

    openTongjiDialog() {
      window.console.log(this.countChazhi);
      let data = [];
      for (let obj in this.countChazhi) {
        window.console.log(obj);
        let keys = obj.split(":");
        let o = {
          key1: keys[0],
          key2: keys[1],
          key3: keys[2],
          value: this.countChazhi[obj],
        };
        data.push(o);
      }
      this.tongjiTabledata = data;
      this.dialogTongjiFormVisible = true;
    },
    async getlenre() {
      const res = await getLengSysCaipiaoNumbers(this.formData);
      if (res.code == 0) {
        this.formlenreData = res.data.list;
      }
    },
    getObjValue(obj) {
      //把对象属性的值转换为array
      var arr = [];
      for (let i in obj) {
        arr.push(obj[i]); //属性
      }
      return arr;
    },
    getObjsToArray(objs) {
      //把大量对象的属性转化为列表。
      for (let obj in objs) {
        objs[obj]["dataArrays"] = this.getObjValue(objs[obj]);
      }
      window.console.log(objs);
      return objs;
    },
    suanfa() {
      //开始按照规律1统计算法。
      this.countChazhi = {};
      this.anumbercount = 0;
      this.tableData = this.getObjsToArray(this.tableData);
      for (let obj in this.tableData) {
        const arr = this.tableData[obj]["dataArrays"];
        if (arr.indexOf(this.formData.anumber) > -1) {
          this.anumbercount = this.anumbercount + 1;
          if (parseInt(obj) < this.formData.tabley) {
            continue;
            window.console.log("跳出循环", arr);
          }
          //查找当前比较值是否存在在列表中。
          //灰太狼1号：计算a 值的x行，y列 的所有位置-d值，在z1行出现多少次。
          for (let i = 0; i < this.formData.tabley; i++) {
            let xnumberdataArrays =
              this.tableData[parseInt(obj) - i - 1]["dataArrays"]; //从0开始数所以要减1
            let xnumberdataArray = xnumberdataArrays.slice(4, 9);
            for (let j in xnumberdataArray) {
              //xnumberdata为a值上5列进行循环
              let xnumberdata = xnumberdataArray[j];
              let number =
                parseInt(xnumberdata) + parseInt(this.formData.dnumber);
              window.console.log("此处统计", j, number);
              for (let l = 0; l < this.formData.tabley2; l++) {
                //循环a值下方，看指定行出现的数据
                if (parseInt(obj) + l + 1 - this.tableData.length >= 0) {
                  window.console.log(parseInt(obj) + l + 1, obj);
                  continue;
                }
                let y2arry =
                  this.tableData[parseInt(obj) + l + 1]["dataArrays"];
                if (y2arry.indexOf(number.toString()) > -1) {
                  // key=2-2-4   2为往上期数  2为列数  4为期数
                  let key =
                    (i + 1).toString() +
                    ":" +
                    (xnumberdataArray.indexOf(xnumberdata) + 1).toString() +
                    ":" +
                    (l + 1).toString();
                  if (this.countChazhi.hasOwnProperty(key)) {
                    this.countChazhi[key] = this.countChazhi[key] + 1;
                  } else {
                    this.countChazhi[key] = 1;
                  }
                }
              }
            }
          }
        }
      }
      window.console.log(this.countChazhi);
      window.console.log(this.fudongMessage);
      //this.getTableData();
      this.openTongjiDialog();
    },
  },
  async created() {
    this.page = 1;
    this.pageSize = 100000;
    await this.getTableData();
    await this.getDict("sys_project_id");
  },
};
</script>

<style>
</style>
